package homework12月9号补交.homework2.d12;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.lang.reflect.Constructor;
import java.util.*;

public class test {
    public static void main(String[] args) throws Exception {
        ArrayList<Person> plist = new ArrayList<>();
        HashMap<String, String> map = new HashMap<>();

        List<String> list = FileUtils.readLines(new File("D:\\ldea dashuju\\day.12.6\\src\\czxy\\homework2\\d12\\data1.txt"));

        //
        for (int i = 0; i < list.size(); i++) {
            String[] arr = list.get(i).split("=");
            map.put(arr[0], arr[1]);
        }

        for (Map.Entry<String, String> s : map.entrySet()) {
            List<String> list1 = FileUtils.readLines(new File("D:\\ldea dashuju\\day.12.6\\src\\czxy\\homework2\\d12\\data2.txt"));

            for (String s1 : list1) {
                if (s1.contains(s.getKey())) {
                    Class<?> aClass = Class.forName(s.getValue());
                    Constructor<?> constructor = aClass.getConstructor();
                    Object o = constructor.newInstance();

                    if (o instanceof Student) {
                        Student o1 = (Student) o;
                        s1 = s1.replace("\"", "");
                        System.out.println(s1);
                        String[] arr1 = s1.split(" ");
                        System.out.println(Arrays.toString(arr1));

                        String[] arr2 = arr1[1].split("=");
                        System.out.println(Arrays.toString(arr2));
                        System.out.println(arr2[1]);
                        o1.setId(arr2[0]);

                        String[] arr3 = arr1[2].split("=");
                        System.out.println(arr3[1]);
                        o1.setName(arr3[1]);
                        String[] arr4 = arr1[3].split("=");
                        String str = arr4[1].substring(0,2);
                        System.out.println(str);
                        o1.setAge(Integer.parseInt(str));
                        plist.add(o1);
                    } else if (o instanceof User){
                        User o1 = (User) o;
                        s1 = s1.replace("\"", "");
                        System.out.println(s1);
                        String[] arr1 = s1.split(" ");
                        System.out.println(Arrays.toString(arr1));

                        String[] arr2 = arr1[1].split("=");
                        System.out.println(Arrays.toString(arr2));
                        System.out.println(arr2[1]);
                        o1.setId(arr2[0]);

                        String[] arr3 = arr1[2].split("=");
                        System.out.println(arr3[1]);
                        o1.setName(arr3[1]);
                        String[] arr4 = arr1[3].split("=");
                        String str = arr4[1].substring(0,2);
                        System.out.println(str);
                        o1.setAge(Integer.parseInt(str));
                        plist.add(o1);
                    }
                }
            }
        }
    }
}
